package com.naihe.anderson.sms.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.ContactUser;
import com.naihe.anderson.sms.entity.Nhsms;
import com.naihe.anderson.sms.manager.ContactMgr;

public class SearchMsgAdapter extends BaseAdapter{

    Context mContext;
    List<Nhsms> objects;
    Date nowDate;
    ContactMgr contactMgr;
    String searchKey = "";

    public SearchMsgAdapter(Context context,List<Nhsms> objects) {
        mContext = context;
        this.objects = objects;
        nowDate = new Date();
        contactMgr = ((NhSmsApp)mContext.getApplicationContext()).getContactMgr();
    }

    @Override
	public int getCount() {
		if(objects == null) return 0;
		return objects.size();
	}

	@Override
	public Nhsms getItem(int position) {
		if(objects == null) return null;
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View view = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            view = inflater.inflate(R.layout.search_msg_item, null);
        } else {
            view = convertView;
        }
        Nhsms entityObj = objects.get(position);

        TextView time = (TextView) view.findViewById(R.id.system_time);
        TextView typeName = (TextView) view.findViewById(R.id.typeName);
        TextView content = (TextView) view.findViewById(R.id.system_msg_content);

        time.setText(getDataFormat(entityObj.getDate()));
        
        SpannableString sp = new SpannableString(entityObj.getBody());
		int begin = entityObj.getBody().indexOf(searchKey);
		int end = begin + searchKey.length();
		sp.setSpan(new ForegroundColorSpan(0xffff9900), begin, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);   
        content.setText(sp);
        
        ContactUser contactUser = contactMgr.getContactUserById(contactMgr.getContactIdByPhoneNum(entityObj.getAddress()));
		if(contactUser != null){
			typeName.setText(contactUser.getName());
		} else {
			typeName.setText(entityObj.getAddress());
		}
        
        //
		contactUser = null;
        entityObj = null;
        

        return view;
    }
    
    public void setSearchKey(String searchKey){
    	this.searchKey = searchKey;
    }

    public String getOldSearchKey(){
    	return searchKey;
    }
    
    private String getDataFormat(long time){
        String formatTime = "";
        Date date = new Date(time);
        try {
            if(date.getYear()==nowDate.getYear() && date.getMonth()==nowDate.getMonth() && date.getDate()==nowDate.getDate()){
				formatTime = mContext.getResources().getString(R.string.today) + new SimpleDateFormat("HH:mm").format(date);
			}else if(date.getYear()==nowDate.getYear() && date.getMonth()==nowDate.getMonth() && date.getDate()==(nowDate.getDate()-1)){
				formatTime = mContext.getResources().getString(R.string.yesterday) + new SimpleDateFormat("HH:mm").format(date);
			}else if(date.getYear()==nowDate.getYear()){
				formatTime = new SimpleDateFormat("MM-dd").format(date);
			}else{
				formatTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
			}
        } catch (Exception e) {
            formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);;
        }
        return formatTime;
    }
}
